﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using com.vs.DataAccess;
using com.vs.General;

namespace com.vs.DataAccess.DO
{
    public partial class TaskHeaderDO : BaseDO
    {

        #region "Get Model"
        /// <summary>
        /// 将符合数据库主键的持久对象提取到模型对象中，如数据库符合条件的持久对象不存在，则返回空对象
        /// </summary>
        /// <remarks></remarks>
        public static TaskHeaderDO GetModelByPK(Session sess, int iIntInstrNum)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = SelectSequel + " WHERE INTERNAL_TASK_NUM = @INTERNAL_TASK_NUM ";
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.Text, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_TASK_NUM", iIntInstrNum)
                });
                if (!DataManager.IsEmpty(dtModel))
                {
                    return GetModel(dtModel.Rows[0]);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }


        }

        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据，同时自增长属性INTERNAL_TASK_NUM也会同时得到更新,并提供数据库事务支持
        /// </summary>
        /// <remarks></remarks>
        public int Insert(Session sess)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "TSK_ITaskHeader";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(this);
                return helper.GetInt(CommandType.StoredProcedure, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        public void Delete(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "TSK_DTaskHeader";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, UpdateWherePara(this));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public string Update(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "TSK_UTaskHeader";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(this));
                return null;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string Update(Session sess, Dictionary<string, object> dcData)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            string stSQL = null;
            int i = 0;
            IDataParameter[] paras = null;

            try
            {
                if (dcData.Count < 1)
                    return null;
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(256)
                            .Append(" UPDATE TASK_HEADER SET ");
                paras = new IDataParameter[dcData.Count + 1];
                foreach (KeyValuePair<string, object> kvp in dcData)
                {
                    sbSQL.Append(kvp.Key).Append(" = @Value").Append(i).Append(",");
                    paras[i] = new SqlParameter("@Value" + i, kvp.Value);
                    i++;
                }

                stSQL = sbSQL.ToString();
                stSQL = stSQL.Substring(0, stSQL.Length - 1);

                stSQL = stSQL + " WHERE INTERNAL_TASK_NUM = @INTERNAL_TASK_NUM";
                paras[i] = new SqlParameter("@INTERNAL_TASK_NUM", this.INTERNAL_TASK_NUM);

                helper.Update(CommandType.Text, stSQL, paras);

                return null;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象WORKTASK的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        public static List<TaskHeaderDO> GetAll(Session sess)
        {
            DataHelper helper = null;
            DataTable dtModel = null;

            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.Text, SelectSequel);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性WORKTASK对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static DataTable GetTableByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = null;

            try
            {
                helper = new DataHelper(sess);
                stSQL = SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
                    new SqlParameter( "@Value", value)
                };
                return helper.GetTable(CommandType.Text, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public static List<TaskHeaderDO> GetListByColumn(Session sess, string columnName, Object value)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumn(sess, columnName, value);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
            }
        }


        /// <summary>
        /// 从DataView装载持久性WORKTASK对象到集合
        /// </summary>
        /// <remarks></remarks>
        private static List<TaskHeaderDO> LoadListFromDataView(DataView dv)
        {
            List<TaskHeaderDO> list = new List<TaskHeaderDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }

        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        public static TaskHeaderDO GetModel(DataRow row)
        {
            TaskHeaderDO workDO = new TaskHeaderDO();
            if (!row["TASK_TYPE"].Equals(DBNull.Value)) workDO.TASK_TYPE = Convert.ToString(row["TASK_TYPE"]);
            if (!row["INTERNAL_TASK_NUM"].Equals(DBNull.Value)) workDO.INTERNAL_TASK_NUM = Convert.ToInt32(row["INTERNAL_TASK_NUM"]);
            if (!row["TASK_ID"].Equals(DBNull.Value)) workDO.TASK_ID = Convert.ToString(row["TASK_ID"]);
            if (!row["CONDITION"].Equals(DBNull.Value)) workDO.CONDITION = Convert.ToString(row["CONDITION"]);
            if (!row["REFERENCE_ID"].Equals(DBNull.Value)) workDO.REFERENCE_ID = Convert.ToString(row["REFERENCE_ID"]);
            if (!row["REFERENCE_NUM_TYPE"].Equals(DBNull.Value)) workDO.REFERENCE_NUM_TYPE = Convert.ToString(row["REFERENCE_NUM_TYPE"]);
            if (!row["REFERENCE_NUM"].Equals(DBNull.Value)) workDO.REFERENCE_NUM = Convert.ToInt32(row["REFERENCE_NUM"]);
            if (!row["COMPANY"].Equals(DBNull.Value)) workDO.COMPANY = Convert.ToString(row["COMPANY"]);
            if (!row["WAREHOUSE"].Equals(DBNull.Value)) workDO.WAREHOUSE = Convert.ToString(row["WAREHOUSE"]);
            if (!row["CONFIRM_USER"].Equals(DBNull.Value)) workDO.CONFIRM_USER = Convert.ToString(row["CONFIRM_USER"]);
            if (!row["ASSIGNED_USER"].Equals(DBNull.Value)) workDO.ASSIGNED_USER = Convert.ToString(row["ASSIGNED_USER"]);
            if (!row["USER_STAMP"].Equals(DBNull.Value)) workDO.USER_STAMP = Convert.ToString(row["USER_STAMP"]);
            if (!row["DATE_TIME_STAMP"].Equals(DBNull.Value)) workDO.DATE_TIME_STAMP = Convert.ToDateTime(row["DATE_TIME_STAMP"]);
            if (!row["INTERNAL_TASK_TYPE"].Equals(DBNull.Value)) workDO.INTERNAL_TASK_TYPE = Convert.ToString(row["INTERNAL_TASK_TYPE"]);
            if (!row["HOLD_CODE"].Equals(DBNull.Value)) workDO.HOLD_CODE = Convert.ToString(row["HOLD_CODE"]);
            if (!row["INTERNAL_WAVE_NUM"].Equals(DBNull.Value)) workDO.INTERNAL_WAVE_NUM = Convert.ToInt32(row["INTERNAL_WAVE_NUM"]);

            return workDO;
        }

        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        private static string SelectSequel
        {
            get
            {
                return "Select	*  From [TASK_HEADER] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDbDataParameter[] ValueParas(TaskHeaderDO workDO)
        {
            SqlParameter[] paras = new SqlParameter[16];
            paras[0] = new SqlParameter("@TASK_TYPE", workDO.TASK_TYPE == String.Empty ? DBNull.Value : (object)workDO.TASK_TYPE);
            paras[1] = new SqlParameter("@INTERNAL_TASK_NUM", workDO.INTERNAL_TASK_NUM == 0 ? DBNull.Value : (object)workDO.INTERNAL_TASK_NUM);
            paras[2] = new SqlParameter("@TASK_ID", workDO.TASK_ID == String.Empty ? DBNull.Value : (object)workDO.TASK_ID);
            paras[3] = new SqlParameter("@CONDITION", workDO.CONDITION == String.Empty ? DBNull.Value : (object)workDO.CONDITION);
            paras[4] = new SqlParameter("@REFERENCE_ID", workDO.REFERENCE_ID == String.Empty ? DBNull.Value : (object)workDO.REFERENCE_ID);
            paras[5] = new SqlParameter("@REFERENCE_NUM_TYPE", workDO.REFERENCE_NUM_TYPE == String.Empty ? DBNull.Value : (object)workDO.REFERENCE_NUM_TYPE);
            paras[6] = new SqlParameter("@REFERENCE_NUM", workDO.REFERENCE_NUM);
            paras[7] = new SqlParameter("@COMPANY", workDO.COMPANY == String.Empty ? DBNull.Value : (object)workDO.COMPANY);
            paras[8] = new SqlParameter("@WAREHOUSE", workDO.WAREHOUSE == String.Empty ? DBNull.Value : (object)workDO.WAREHOUSE);
            paras[9] = new SqlParameter("@CONFIRM_USER", workDO.CONFIRM_USER == String.Empty ? DBNull.Value : (object)workDO.CONFIRM_USER);
            paras[10] = new SqlParameter("@USER_STAMP", workDO.USER_STAMP == String.Empty ? DBNull.Value : (object)workDO.USER_STAMP);
            paras[11] = new SqlParameter("@DATE_TIME_STAMP", workDO.DATE_TIME_STAMP == DateTime.MinValue ? DBNull.Value : (object)workDO.DATE_TIME_STAMP);
            paras[12] = new SqlParameter("@INTERNAL_TASK_TYPE", workDO.INTERNAL_TASK_TYPE == String.Empty ? DBNull.Value : (object)workDO.INTERNAL_TASK_TYPE);
            paras[13] = new SqlParameter("@ASSIGNED_USER", workDO.ASSIGNED_USER == String.Empty ? DBNull.Value : (object)workDO.ASSIGNED_USER);
            paras[14] = new SqlParameter("@HOLD_CODE", workDO.HOLD_CODE == String.Empty ? DBNull.Value : (object)workDO.HOLD_CODE);
            paras[15] = new SqlParameter("@INTERNAL_WAVE_NUM", workDO.INTERNAL_WAVE_NUM);


            paras[0].DbType = DbType.String;
            paras[1].DbType = DbType.Decimal;
            paras[2].DbType = DbType.String;
            paras[3].DbType = DbType.String;
            paras[4].DbType = DbType.String;
            paras[5].DbType = DbType.String;
            paras[6].DbType = DbType.Decimal;
            paras[7].DbType = DbType.String;
            paras[8].DbType = DbType.String;
            paras[9].DbType = DbType.String;
            paras[10].DbType = DbType.String;
            paras[11].DbType = DbType.DateTime;
            paras[12].DbType = DbType.String;
            paras[13].DbType = DbType.String;
            paras[14].DbType = DbType.String;
            paras[15].DbType = DbType.Decimal;


            return paras;
        }

        private static IDbDataParameter[] UpdateWherePara(TaskHeaderDO workDO)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@INTERNAL_TASK_NUM", workDO.INTERNAL_TASK_NUM);
            return paras;
        }

        #endregion

        #region CopyTo
        public TaskHeaderDO CopyTo()
        {
            TaskHeaderDO workDO = new TaskHeaderDO();
            workDO.TASK_TYPE = this.TASK_TYPE;
            workDO.TASK_ID = this.TASK_ID;
            workDO.CONDITION = this.CONDITION;
            workDO.REFERENCE_ID = this.REFERENCE_ID;
            workDO.REFERENCE_NUM_TYPE = this.REFERENCE_NUM_TYPE;
            workDO.REFERENCE_NUM = this.REFERENCE_NUM;
            workDO.COMPANY = this.COMPANY ;
            workDO.WAREHOUSE = this.WAREHOUSE;
            workDO.CONFIRM_USER = this.CONFIRM_USER;
            workDO.ASSIGNED_USER = this.ASSIGNED_USER;
            workDO.USER_STAMP = this.USER_STAMP ;
            workDO.DATE_TIME_STAMP = System.DateTime.Now ;
            workDO.INTERNAL_TASK_TYPE = this.INTERNAL_TASK_TYPE;
            return workDO;
        }
        #endregion

        #region GetValueFromFieldName
        public string GetValueFromFieldName(string stFieldName)
        {
            string fieldValue = null;

            switch (stFieldName)
            {
                case "TASK_TYPE":
                    fieldValue = this.TASK_TYPE;
                    break;
                case "INTERNAL_TASK_NUM":
                    fieldValue = this.INTERNAL_TASK_NUM + "";
                    break;
                case "TASK_ID":
                    fieldValue = this.TASK_ID;
                    break;
                case "CONDITION":
                    fieldValue = this.CONDITION;
                    break;
                case "REFERENCE_ID":
                    fieldValue = this.REFERENCE_ID;
                    break;
                case "REFERENCE_NUM_TYPE":
                    fieldValue = this.REFERENCE_NUM_TYPE;
                    break;
                case "REFERENCE_NUM":
                    fieldValue = this.REFERENCE_NUM + "";
                    break;
                case "COMPANY":
                    fieldValue = this.COMPANY;
                    break;
                case "ITEM":
                case "WAREHOUSE":
                    fieldValue = this.WAREHOUSE;
                    break;
                case "CONFIRM_USER":
                    fieldValue = this.CONFIRM_USER;
                    break;
                case "ASSIGNED_USER":
                    fieldValue = this.ASSIGNED_USER;
                    break;
                case "USER_STAMP":
                    fieldValue = this.USER_STAMP;
                    break;
                case "DATE_TIME_STAMP":
                    fieldValue = this.DATE_TIME_STAMP + "";
                    break;
                case "INTERNAL_TASK_TYPE":
                    fieldValue = this.INTERNAL_TASK_TYPE;
                    break;
            }
            return fieldValue;
        }
        #endregion


        public static List<TaskHeaderDO> GetListByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumns(sess, dcParam, stOrderBy);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
            }
        }

        public static DataTable GetTableByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(SelectSequel)
                    .Append(" WHERE 1=1 ");

                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key.ToString()).Append(de.Value.Key).Append("@Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value.Value);
                    i++;
                }

                if (!StringManager.IsEmpty(stOrderBy))
                {
                    sbSQL.Append(stOrderBy);
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
    }
}
